/*
 * Android Simple Music player
   Copyright hayoon.ko (shauku@naver.com)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package kr.ac.kaist.simplemusicplayer;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Music List adapter - display various information in one musiclist array 
 * @author shauku@naver.com
 * @version 10
 *
 */
class MusicListAdapter extends ArrayAdapter<MusicInfoItem> {

	private int resource;
	private LayoutInflater inflater;
	private Context context;
	
	int kindsOfMenu;
	
	public MusicListAdapter(Context ctx, int resourceId, List<MusicInfoItem> objects){
		super(ctx, resourceId, objects);
		resource = resourceId;
		inflater = LayoutInflater.from(ctx);
		context = ctx;
	}
	
	@Override 
	public View getView(int position, View convertView, ViewGroup parent){
		convertView = (LinearLayout)inflater.inflate(resource,null);
		
		MusicInfoItem musicInfo = getItem(position);
		
		TextView txtName = (TextView)convertView.findViewById(R.id.search_list_txt_name);
		txtName.setText(musicInfo.getTitle());	

		TextView txtAuthor = (TextView)convertView.findViewById(R.id.search_list_txt_author);
		txtAuthor.setText(musicInfo.getAuthor());	
		
		ImageView ambumArt = (ImageView)convertView.findViewById(R.id.music_item_img);
		String albumId = musicInfo.getAlbumid();
		
		 Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
		 Uri uri = ContentUris.withAppendedId(sArtworkUri, Integer.valueOf(albumId));
		
		 ContentResolver res = context.getContentResolver();				

		 InputStream in;
         try {
        	 
        	 BitmapFactory.Options resizeOpts = new Options();
        	 resizeOpts.inSampleSize = 2;
        	 
             in = res.openInputStream(uri);
         	  Bitmap artwork = BitmapFactory.decodeStream(in, null, resizeOpts);	
		      Bitmap temp_art = Bitmap.createScaledBitmap(artwork, 40, 40, true);
		      ambumArt.setImageBitmap(artwork);
         } catch (FileNotFoundException e) {
                 e.printStackTrace();
		     	ambumArt.setImageResource(R.drawable.albumart_unknown);		                  
         }     		          			

		return convertView;
	}
	
}
